import { createRouter, createWebHistory } from 'vue-router'
// import HomeView from '../views/HomeView.vue'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            component: () => import('@/views/Login.vue'),
        },
        {
            path: '/layout',
            component: () => import('@/views/Layout.vue'),
            children: [
                {
                    path: '/index',
                    component: () => import('@/views/layout/Index.vue'),
                },
                {
                    path: '/guanli',
                    component: () => import('@/views/layout/GuanLi.vue'),
                    children: [
                        {
                            path: '/bumen',
                            component: () => import('@/views/layout/guanli/BuMen.vue'),
                        },
                        {
                            path: '/gangwei',
                            component: () => import('@/views/layout/guanli/GangWei.vue'),
                        },
                        {
                            path: '/rizhi',
                            component: () => import('@/views/layout/guanli/RiZhi.vue'),
                            children: [
                                {
                                    path: '/denglu',
                                    component: () => import('@/views/layout/guanli/rizhi/DengLu.vue'),
                                },
                                {
                                    path: '/caozuo',
                                    component: () => import('@/views/layout/guanli/rizhi/CaoZuo.vue'),
                                },
                            ]
                        },
                    ]
                },
                {
                    path: '/jiankong',
                    component: () => import('@/views/layout/JianKong.vue'),
                },
            ]
        },
    ],
})

router.beforeEach((to) => {
    // 从本地存储空间获取token
    let token = localStorage.getItem('token')
    // 未登录，不能访问其他页面
    if (!token && to.path !== '/login') return '/login'
    // 登录后，不能访问登录页
    if (token && to.path === '/login') return '/index'
})

export default router
